#!/bin/bash

SOURCE_PATH=coelacanth/
SAMPLE_PATH=src/
DOC_PATH=doc-coelacanth/

gen_lib_doc()
{
   HTML=$DOC_PATH/$1.html

   echo "
   <html>
   <head>
   <style type="text/css">
   body { font-size: 10pt; font-family: Arial; }
   table { width: 70%;font-size: 10pt; font-family: Arial; 
           overflow: auto; border-spacing:0;border-collapse:collapse}
   td { font-size: 10pt; font-family: Arial; border: solid 1px black;}
   td.t { font-size: 10pt; font-family: Arial; border: solid 1px black; 
          background: lightgrey; }
   A:link {text-decoration: none; color: darkblue}
   A:visited {text-decoration: none; color: darkblue}
   A:active {text-decoration: none; color: darkblue}
   A:hover {text-decoration: none; color: darkgray;}
   </style>
   </head>
   <body>
   " > $HTML

   CFILE=`echo $1 | sed -e 's/\.h/.c/g'`

   echo "<h1>&lt;coelacanth/$1&gt;</h1>" >> $HTML
   echo "Sample File: <a href='$CFILE.html'>$CFILE</a><br><br>" >> $HTML



   cat $1 | grep "* @" >> $HTML

   sed -i 's/ \* @func:\(.*\)$/<table><tr><td class="t" colspan="2"><b>\1<\/b><\/td><\/tr>/g' $HTML
   
   sed -i 's/ \* @desc:\(.*\)$/<tr><td colspan="2"><i>\1<\/i><\/td><\/tr>/g' $HTML
   
   sed -i 's/ \* @param\(.*\):\(.*\)$/<tr><td width="30%">param \1<\/td><td>\2<\/td><\/tr>/g' $HTML
   
   sed -i 's/ \* @return\(.*\):\(.*\)$/<tr><td>return\1<\/td><td>\2<\/td><\/tr><\/table><br>/g' $HTML


   keywords="char\|const\|int\|size_t\|float\|double\|unsigned"
   sed -i "s/\([ ]\)\($keywords\)\([ ]\)/\1<font color='darkgreen'>\2<\/font>\3/g" $HTML
   
   #sed -i 's/ const/<font color="darkgreen"> const<\/font>/g' $HTML
   #sed -i 's/ int/<font color="darkgreen"> int<\/font>/g' $HTML
   #sed -i 's/ size_t/<font color="darkgreen"> size_t<\/font>/g' $HTML
   #sed -i 's/ float/<font color="darkgreen"> float<\/font>/g' $HTML
   #sed -i 's/ double/<font color="darkgreen"> double<\/font>/g' $HTML
   #sed -i 's/ unsigned/<font color="darkgreen"> unsigned<\/font>/g' $HTML

   sed -i 's/param/<font color="#27406d">param<\/font>/g' $HTML
   sed -i 's/return/<font color="#6d4d00">return<\/font>/g' $HTML

   echo "</body></html>" >> $HTML
}


gen_sample_doc()
{
   HTML=$DOC_PATH/$1.html


   echo "
   <html>
   <head>
   <style type="text/css">
   body { font-size: 10pt; font-family: Arial; }
   </style>
   </head>
   <body>
   " > $HTML

   cat $1 | sed -e 's/</\&lt;/g' | sed -e "s/>/\&gt;/g" | sed -e 's/$/<\/br>/g' | sed 's/\t/\&nbsp;\&nbsp;\&nbsp;/g' | sed 's/ /\&nbsp;/g' >> $HTML

   sed -i 's/char/<font color="darkgreen">char<\/font>/g' $HTML
   sed -i 's/const/<font color="darkgreen">const<\/font>/g' $HTML
   sed -i 's/int/<font color="darkgreen">int<\/font>/g' $HTML
   sed -i 's/size_t/<font color="darkgreen">int<\/font>/g' $HTML
   sed -i 's/float/<font color="darkgreen">float<\/font>/g' $HTML
   sed -i 's/double/<font color="darkgreen">double<\/font>/g' $HTML
   sed -i 's/unsigned/<font color="darkgreen">unsigned<\/font>/g' $HTML
   sed -i 's/unsigned/<font color="darkgreen">unsigned<\/font>/g' $HTML

   sed -i 's/return/<font color="#6d4d00">return<\/font>/g' $HTML
   sed -i 's/#define/<font color="darkred">#define<\/font>/g' $HTML
   sed -i 's/#include/<font color="darkred">#include<\/font>/g' $HTML

   echo "</body></html>" >> $HTML
}




cd $SOURCE_PATH
   SOURCE_PATH=`pwd`
cd ..

cd $DOC_PATH
 DOC_PATH=`pwd`
cd ..

cd $SOURCE_PATH
for file in `ls`
do
   gen_lib_doc $file 
done
cd ..


cd $SAMPLE_PATH
for file in `ls`
do
   gen_sample_doc $file 
done
cd ..



